package com.goblin.model.media.repository;

import com.goblin.common.response.PageHelper;
import com.goblin.common.response.Paged;
import com.goblin.model.base.BaseRepositoryImpl;
import com.goblin.model.media.po.Chat;
import com.goblin.model.media.po.QChat;
import com.goblin.model.media.po.QMessage;
import com.querydsl.core.QueryResults;
import com.querydsl.jpa.JPQLQuery;

import javax.persistence.EntityManager;

public class ChatRepositoryImpl extends BaseRepositoryImpl<Chat, Integer>
        implements ChatRepositoryCustom {

    public ChatRepositoryImpl(EntityManager entityManager) {
        super(Chat.class, entityManager);
    }

    @Override
    public Paged<Chat> paged(Integer userId, PageHelper pageable) {
        JPQLQuery<Chat> query = this.jpaQueryFactory.selectFrom(QChat.chat)
                .where(QChat.chat.deleteTime.isNull()).where(QChat.chat.customA.id.eq(userId))
                .orderBy(QChat.chat.date.desc());
        query = (JPQLQuery<Chat>) getQuerydsl(pageable, query);
        QueryResults<Chat> results = query.fetchResults();
        return new Paged<Chat>(results.getTotal(), results.getResults());
    }
}
